#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2025/1/12
# @Author : Joker
# @Version : Python 3.7
# @Software: PyCharm
import sys
import logging
import allure
from datetime import datetime
from loguru import logger
from utils.path import LOG_PATH


class AllureLogHandler(logging.Handler):
    def emit(self, record):
        timestamp = datetime.fromtimestamp(record.created).strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
        log_entry = self.format(record)
        allure.attach(log_entry, name=timestamp, attachment_type=allure.attachment_type.TEXT)


current_date = datetime.now().strftime("%Y-%m-%d")

logger.configure(handlers=[
    {
        "sink": sys.stderr,
        "format": "{time:YYYY-MM-DD HH:mm:ss.SSS} |<lvl>{level:7}</>| {name}:{module}:{line} - <lvl>{message}</>",
        "colorize": True
    },
    {
        "sink": AllureLogHandler(),
        "format": "{time:YYYY-MM-DD HH:mm:ss.SSS} |{level:7}| {name}:{module}:{line} - {message}",
        "colorize": False
    },
    {
        "sink": f"{LOG_PATH}/{current_date}.log",
        "format": "{time:YYYY-MM-DD HH:mm:ss.SSS} |{level:7}| {name}:{module}:{line} - {message}",
        "colorize": False,
        "rotation": "00:00",
        "retention": "30 days",
        "compression": "zip",
        "enqueue": True
    }
])
